package bean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import org.primefaces.event.SelectEvent;

import dao.ClientDAO;
import dao.JPA;
import dao.TreatmentActDAO;
import entity.Client;
import entity.TreatmentAct;

@ManagedBean
@ViewScoped
public class ViewTreatmentHistoryBean {

	@Inject	@JPA
	private TreatmentActDAO actDAO;
	@Inject	@JPA
	private ClientDAO clientDAO;
	private Client client;
	private List<TreatmentAct> actList = new ArrayList<>();

	@PostConstruct
	private void init() {
		FacesContext context = FacesContext.getCurrentInstance();
		Map<String, String> params = context.getExternalContext().getRequestParameterMap();
		if (params.get("clientId") != null) {
			int clientId = Integer.valueOf(params.get("clientId"));
			client = clientDAO.getEntityById(clientId);
			actList = actDAO.selectActListByUserId(clientId);
		}
	}

	public void onRowSelect(SelectEvent event) {
		int selectedActId = ((TreatmentAct) event.getObject()).getId();
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("viewTreatmentAct.xhtml?actId=" + selectedActId);
		} catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}

	public List<TreatmentAct> getActList() {
		return actList;
	}

	public Client getClient() {
		return client;
	}

	public String goBack() {
		return "viewClientProfile.xhtml?clientId=" + client.getId() + "&faces-redirect=true";
	}

}
